package com.rocketMQ.rocketmqdemo_20230225.controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * @author Administrator
 */

@RestController
@RequestMapping("provider")
public class ProviderController {

    private static final int length = 10;

    @GetMapping("syncMethod")
    public void syncMethod() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //创造生产者对象
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("provider");
        defaultMQProducer.setNamesrvAddr("localhost:9876");
        //启动生产者实例
        defaultMQProducer.start();
        for (int i = 0; i < length; i++) {
            Message message = new Message("TopicTest", "TagA", ("Hello RocketMQ [" + i + "]").getBytes(StandardCharsets.UTF_8));
            //将message信息传递至broker
            SendResult send = defaultMQProducer.send(message,100000);
            System.out.printf("%s%n", send);
        }
        //关闭资源
        defaultMQProducer.shutdown();
        System.out.println("关闭成功");

    }
}
